﻿//-----------------------------------------------------------------------
// <copyright file="HttpClientController.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: HttpClientController.cs
// * history : created by qinchaoyue 2017-11-21 10:34:16
// </copyright>
//-----------------------------------------------------------------------

using System.Threading.Tasks;

namespace WebApplication.Controllers
{
    using Microsoft.AspNetCore.Mvc;

    using Slwy.FrameCore.Helper;
    using Slwy.Mvc.Controller;

    /// <summary>
    /// The http client controller.
    /// </summary>
    [Route("api/test/httpclient")]
    public class HttpClientController : SlwyController
    {
        /// <summary>
        /// The _slwy http client.
        /// </summary>
        private ISlwyHttpClient _slwyHttpClient;

        /// <summary>
        /// Initializes a new instance of the <see cref="HttpClientController"/> class.
        /// </summary>
        /// <param name="slwyHttpClient">
        /// The slwy http client.
        /// </param>
        public HttpClientController(ISlwyHttpClient slwyHttpClient)
        {
            this._slwyHttpClient = slwyHttpClient;
        }

        /// <summary>
        /// 测试调用别的服务
        /// </summary>
        /// <param name="url"> The url.  </param>
        /// <returns> 结果 </returns>
        [HttpPost]
        public async Task<IActionResult> PostService(string url)
        {
            return this.Ok(this._slwyHttpClient.PostString(url));
        }

        /// <summary>
        /// 测试调用别的服务
        /// </summary>
        /// <param name="url"> The url.  </param>
        /// <returns> 结果 </returns>
        [HttpPost("api")]
        public async Task<IActionResult> PostApi(string url)
        {
            return this.Ok(this._slwyHttpClient.PostString(url));
        }

        /// <summary>
        /// 测试调用别的服务
        /// </summary>
        /// <param name="url"> The url.  </param>
        /// <returns> 结果 </returns>
        [HttpGet]
        public async Task<IActionResult> GetService(string url)
        {
            return this.Ok(this._slwyHttpClient.GetString(url, new { accountId = "1703011351229600c1300238416" }));
        }

        /// <summary>
        /// 测试使用 put 方式调用别的服务
        /// </summary>
        /// <returns>调用结果</returns>
        [HttpPut]
        public IActionResult Put()
        {
            var url = "/api/demo/user/recycled";

            return this.Ok(this._slwyHttpClient.PutString(url, new { keyId = "string", isDelete = true }));
        }
    }
}
